package com.gd.luxiaoyu.upfileServlet;

import com.gd.luxiaoyu.moduels.entity.PictrueTableEntity;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "userRegisterServlet",value = "/userRegisterServlet")
public class userCusotmerRegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PictrueTableEntity pictrueTableEntity = null;


        /*这个是用户注册的基本信息  用户在注册之后就会产生一个id
        Integer Uid = Integer.parseInt((String) req.getAttribute(""));
        String
        pictrueTableEntity.setUid(1);

        ServletContext  sc = req.getSession().getServletContext();
        XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext) WebApplicationContextUtils.findWebApplicationContext(sc);
        PictrueTableService appVerService = (PictrueTableService) xmlWebApplicationContext.getBean("PictrueTableServiceImpl");
        BaseMapper<PictrueTableEntity> baseMapper = appVerService.getBaseMapper();

        baseMapper.insert(pictrueTableEntity);*/

        boolean istc = ServletFileUpload.isMultipartContent(req);
        try {
            if (istc) {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                factory.setSizeThreshold(1024 * 100);//文件大小临时存储不能不大于100k
                upload.setFileSizeMax(1024 * 1000 * 10);//上传大小不超过10M

                List<FileItem> items = upload.parseRequest(req);

                Iterator<FileItem> itemIterator = items.iterator();
                while (itemIterator.hasNext()) {
                    FileItem fileItem = itemIterator.next();
                    int sno = -1;
                    String name = null;
                    if (fileItem.isFormField()) {//这里判断是否是表单字段
                        if (fileItem.getFieldName().equals("number")) { //这里是前端传来的账号
                            sno = Integer.parseInt(fileItem.getString("UTF-8"));
                            System.out.println(sno);

                        } else if (fileItem.getFieldName().equals("name")) { //这里是前端传来的名字
                            name = fileItem.getString("UTF-8");

                            System.out.println(name);
                        } else {
                            System.out.println("其他字段。。。。。。。。。。。。");
                        }

                    } else {//表示其他文件  不是普通字段
                        String fileName = fileItem.getName();//获取文件名
                        String etx = fileName.substring(fileName.indexOf(".") + 1);//获取文件的后缀名
                        if (!(etx.equals("png") || etx.equals("jpg") || etx.equals("gif"))) {
                            resp.getWriter().print("你上传的文件有误！请上传“png” 或者 “jpg”  或者 “gif” 格式的文件！！！");
                            return;
                        } else {
                            String path = req.getSession().getServletContext().getRealPath("");//获取服务器路径 并且获取上传的文件路径
                            File file = new File(path, fileName);
                            fileItem.write(file);
                            resp.getWriter().print("成功！！");//这里是反馈给用户的信息  上传成功！！！
                            System.out.println("上传成功！！");
                            return;
                        }

                    }
                    //
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }


}
